<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 权限节点管理
 */
class Admin_node extends SDF_Controller {
    public function __construct(){
    	parent::__construct();
        $this->load->model('admin/admin_node_group_model');
    }
    /**
     * 筛选
     * @param $map
     * @return mixed
     */
    protected function _filter($map){
        $keywords = $this->input->get_post('keywords');
        if(!empty($keywords)){
            if(is_numeric($keywords)){
                $map['where']['id']= $keywords;
            }else{
                $map['like']=array('title'=>$keywords);
            }
        }
        $update_time = $this->input->get_post('update_time');
        if(!empty($update_time)){
            $map['where']["DATE_FORMAT(`update_time`,'%Y-%m-%d') "] =$update_time;
        }

        $create_time = $this->input->get_post('create_time');
        if(!empty($create_time)){
            $map['where']["DATE_FORMAT(`create_time`,'%Y-%m-%d') "] =$create_time;
        }
        unset($map['where']['create_time']);
        unset($map['where']['update_time']);
        return $map;
    }
    /**
     * 新节点-废弃节点管理
     */
    public function mk_admin_node(){
        $this->load->model('admin/admin_node_model');
        #获取未导入的和已经废弃的节点列表
        $this->data['new_node_list'] = $this->admin_node_model->get_new_nodes();
        $this->data['scrap_node_list'] = $this->admin_node_model->get_scrap_nodes();
        //print_r($this->data);
    	$this->load->view('admin/admin_node/mk_admin_node',$this->data);
    }

    /**
     * 导入权限节点
     * @param string $key
     */
    public function insert($key=''){
        $this->load->model('admin/admin_node_model');
        $this->data['new_node_list'] = $this->admin_node_model->get_new_nodes();
        $install_num = 0;
        if($key == 'all'){
            foreach($this->data['new_node_list'] as $node){
                if($this->admin_node_model->_insert($node)){
                    $install_num++;
                }
            }
        }else{
            $key = $this->input->get_post('key');
            if(isset($this->data['new_node_list'][$key])){
                $node  = $this->data['new_node_list'][$key];
                if($this->admin_node_model->_insert($node)){
                    $install_num++;
                }
            }
        }
        if($install_num>0){
            $this->_success(array('callbackType'=>'forward'),"成功导入条（{$install_num}）权限");
        }
        $this->_error('');
    }

    /**
     * 删除指定权限节点
     * @param $id
     */
    public function delete($id){
        $this->load->model('admin/admin_node_model');
        $this->admin_node_model->_from($this->table_name);
        $affected_rows = 0;
        if($id=='all'){
            $this->_error('功能没有实现');
        }else if(is_numeric($id)){
            $affected_rows = $this->admin_node_model->_delete(array('id'=>$id));
        }else{
            $this->_error('参数错误！');
        }
        $this->_success(array('callbackType'=>'forward'),'成功删除:'.$affected_rows.'个权限节点');
    }
}